package simulator.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 * Swing GUI main class for OS-Simulator.
 * @author Kyle Loomis
 *
 */
public class Simulation
{
   private static final long serialVersionUID = 1L;

   private static int delay = 200;

   
   /**
    * Main method for OS-Simulator.  Initializes GUI.
    * @param args Command line arguments for OS-Simulator.  None currently supported.
    */
   public static void main(String[] args)
   {
      // Schedule a job for the event-dispatching thread:
      // creating and showing this application's GUI.
      SwingUtilities.invokeLater(new Runnable()
      {
         public void run()
         {
            createAndShowGUI();
         }
      });
   }

   private static void setStandardLookAndFeel()
   {
      try
      {
         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   }

   /**
    * Create the GUI and show it. For thread safety, this method should be
    * invoked from the event-dispatching thread.
    */
   private static void createAndShowGUI()
   {
      SimulationController controller = new SimulationController();

      // Create and set up the window.
      JFrame frame = new JFrame("OS Simulator");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      setStandardLookAndFeel();
      frame.setLayout(new GridBagLayout());

      Welcome canvas = new Welcome(controller);

      JMenuBar menuBar = new JMenuBar();

      //CreateScenarioAction newAction = new CreateScenarioAction(frame);
      OpenAction openAction = new OpenAction(controller, frame);
      RunAction runAction = new RunAction(controller);
      StopAction stopAction = new StopAction(controller);
      ResumeAction resumeAction = new ResumeAction(controller);
      PauseAction pauseAction = new PauseAction(controller);
      ExitAction exitAction = new ExitAction();
      AboutAction aboutAction = new AboutAction(frame);
      PreferencesAction preferencesAction = new PreferencesAction(controller, frame);

      runAction.setEnabled(false);
      stopAction.setEnabled(false);
      resumeAction.setEnabled(false);
      pauseAction.setEnabled(false);

      JMenu simulation = new JMenu("Simulator");
      //simulation.add(newAction);
      simulation.add(openAction);
      simulation.addSeparator();
      simulation.add(runAction);
      simulation.add(pauseAction);
      simulation.add(stopAction);
      //simulation.add(resumeAction);
      simulation.addSeparator();
      simulation.add(exitAction);
      menuBar.add(simulation);

      JMenu options = new JMenu("Options");
      options.add(preferencesAction);
      menuBar.add(options);

      JMenu help = new JMenu("Help");
      help.add(aboutAction);
      menuBar.add(help);

      frame.setJMenuBar(menuBar);

      JPanel tools = new JPanel();
      tools.setLayout(new BorderLayout());

      JToolBar toolBar = new JToolBar();
      //toolBar.add(newAction);
      toolBar.add(openAction);
      toolBar.addSeparator();
      toolBar.add(runAction);
      toolBar.add(pauseAction);
      toolBar.add(stopAction);
      //toolBar.add(resumeAction);

      tools.add(toolBar);

      GridBagConstraints toolConstraints = new GridBagConstraints();
      toolConstraints.fill = GridBagConstraints.BOTH;
      toolConstraints.weightx = 1;
      toolConstraints.weighty = 0;
      toolConstraints.gridwidth = 3;
      toolConstraints.gridheight = 1;
      toolConstraints.gridx = 0;
      toolConstraints.gridy = 0;

      canvas.setLayout(new GridBagLayout());
      GridBagConstraints animationConstraints = new GridBagConstraints();
      animationConstraints.fill = GridBagConstraints.BOTH;
      animationConstraints.weightx = 1;
      animationConstraints.weighty = 1;
      animationConstraints.gridwidth = 3;
      animationConstraints.gridheight = 1;
      animationConstraints.gridx = 0;
      animationConstraints.gridy = 2;
      canvas.setBackground(Color.WHITE);

      CycleCounter cycleCounter = new CycleCounter(controller);
      ScenarioHeader headerComp = new ScenarioHeader(controller);

      JPanel header = new JPanel();
      header.setLayout(new BorderLayout());
      header.setBackground(Color.WHITE);
      header.add(headerComp, BorderLayout.LINE_START);
      header.add(cycleCounter, BorderLayout.LINE_END);

      GridBagConstraints headerConstraints = new GridBagConstraints();
      headerConstraints.fill = GridBagConstraints.BOTH;
      headerConstraints.weighty = 0;
      headerConstraints.weightx = 1;
      headerConstraints.gridwidth = 3;
      headerConstraints.gridheight = 1;
      headerConstraints.gridx = 0;
      headerConstraints.gridy = 1;
      header.setMinimumSize(new Dimension(300, 70));

      frame.getContentPane().add(tools, toolConstraints);
      frame.getContentPane().add(header, headerConstraints);
      frame.getContentPane().add(canvas, animationConstraints);

      controller.setDelay(delay);
      controller.setFrame(frame);
      controller.setDisplay(canvas);

      // Display the window.
      frame.pack();
      frame.setVisible(true);
      frame.setSize(1024, 700);
   }
}
